import Vue from 'vue';
const store = window.sessionStorage;
const local = window.localStorage;
class StoreMethod {
  set(key, val, localSave) {
    if (key && typeof key === 'string') {
      if (localSave) {
        local.setItem(key, JSON.stringify(val));
      } else {
        store.setItem(key, JSON.stringify(val));
        // 设置所有的缓存key
        if (this.has("storageKey")) {
          let storageKey = this.get("storageKey");
          if (key !== "storageKey" && storageKey.indexOf(key) === -1) {
            storageKey.push(key);
            this.set("storageKey", storageKey)
          }
        } else {
          let storageKey = [];
          storageKey.push(key);
          this.set("storageKey", storageKey)
        }
      }
    }
    return this;
  }
  get(key, localSave) {
    const value = localSave ? local.getItem(key) : store.getItem(key);
    if (typeof value !== 'string') {
      return undefined;
    }
    try {
      return JSON.parse(value);
    } catch (e) {
      return value || undefined;
    }
  }
  remove(key, localSave) {
    const val = this.get(key, localSave);
    if (localSave) {
      local.removeItem(key);
    } else {
      store.removeItem(key);
      // 清除缓存key
      if (this.has("storageKey")) {
        let storageKey = this.get("storageKey");
        for (let i = 0; i < storageKey.length; i++) {
          if (storageKey[i] === key) {
            storageKey.splice(i, 1);
            break;
          }
        }
        this.set("storageKey", storageKey)
      }
    }
    return val;
  }
  has(key, localSave) {
    let result = false;
    if (this.get(key, localSave) !== undefined && this.get(key, localSave) !== 'undefined') {
      result = true;
    }
    return result;
  }
  clear() {
    local.clear();
    store.clear();
    return this;
  }
  /**
   * 获取所有的sessionStorage 对象，存到指定localStorage
   */
  setSession2Local() {
    if (this.has("storageKey")) {
      let storageKey = this.get("storageKey");
      let allSessionStorage = [];
      for (let i = 0; i < storageKey.length; i++) {
        let temp = {
          k: storageKey[i],
          v: this.get(storageKey[i])
        }
        allSessionStorage.push(temp);
      }
      this.set("allSessionStorage", allSessionStorage, "local");
    }
  }
  /**
   * 获取指定localStorage 对象，设置到sessionSorage
   */
  getLocal2Session() {
    if (this.has("allSessionStorage", "local")) {
      let allSessionStorage = this.get("allSessionStorage", "local");
      for (let i = 0; i < allSessionStorage.length; i++) {
        this.set(allSessionStorage[i].k, allSessionStorage[i].v);
      }
      this.remove("allSessionStorage", "local")
    }
  }
  // 删除所有的会话缓存
  removeAllSessionStorage() {
    if (this.has("storageKey")) {
      let storageKey = this.get("storageKey");
      for (let i = 0; i < storageKey.length; i++) {
        this.remove(storageKey[i])
      }
    }
  }
}
const storage = new StoreMethod();
export default storage;
Object.defineProperty(Vue.prototype, '$storage', {
  value: storage
});
